package net;

import java.io.IOException;
import java.net.InetAddress;

import java.io.IOException;
import java.net.InetAddress;

import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
import jpcap.packet.EthernetPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.UDPPacket;

public class PacketMockUtil {
    private static JpcapSender sender=null;
    public static byte[] getMac(String mac){
        String[] macs=mac.split("-");
        int len=macs.length;
        byte[] bytes=new byte[len];
        for(int i=0;i<len;i++){
            bytes[i]=(byte)Integer.parseInt(macs[i],16);
        }
        return bytes;
    }
    public static void sendUDPMockPacket(String srcIP,int srcPort,String dstIP,int dstPort,String log,String srcMac,String dstMac) throws IOException{
        if(sender==null){
            NetworkInterface device=JpcapCaptor.getDeviceList()[0];
            sender=JpcapSender.openDevice(device);
        }
        UDPPacket p=new UDPPacket(srcPort,dstPort);
        p.setIPv4Parameter(0,false,false,false,0,false,false,false,0,1010101,127,IPPacket.IPPROTO_UDP,
            InetAddress.getByName(srcIP),InetAddress.getByName(dstIP));
        p.data=log.getBytes();

        EthernetPacket ether=new EthernetPacket();
        ether.frametype=EthernetPacket.ETHERTYPE_IP;
        
        ether.src_mac=getMac(srcMac);
        ether.dst_mac=getMac(dstMac);
        p.datalink=ether;
        sender.sendPacket(p);        
    }
   

}
